package com.gimi.cloud.bbp.client.service.order.impl;

import com.gimi.cloud.bbp.client.service.order.OrderService;
import com.gimi.cloud.bbp.common.enums.CommonEnume;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dao.dao.order.OrderDao;
import com.gimi.cloud.bbp.dao.dao.order.OrderExpressDao;
import com.gimi.cloud.bbp.dao.dao.order.OrderGoodsDao;
import com.gimi.cloud.bbp.dao.dao.order.OrderOperDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dto.order.OrderDTO;
import com.gimi.cloud.bbp.dto.order.OrderExpressDTO;
import com.gimi.cloud.bbp.entity.order.OrderEntity;
import com.gimi.cloud.bbp.entity.order.OrderGoodsEntity;
import com.gimi.cloud.bbp.entity.order.OrderOperEntity;
import com.gimi.cloud.bbp.entity.product.ProductSkuEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单相关服务
 * @author 李林峰
 * @version 1.0
 * @Description
 * @create 2018年1月25日
 */
@Service
public class OrderServiceImpl implements OrderService {
	
	private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
	
	@Autowired private OrderDao orderDao;
	@Autowired private OrderGoodsDao orderGoodsDao;
	@Autowired private EcProdCatModelValueDao prodCatModelValueDao;
	@Autowired private OrderOperDao orderOperDao;
	@Autowired private OrderExpressDao orderExpressDao;

	@Override
	public Response userOrderList(OrderDTO orderDTO) {
		Response response = new Response(ResponseCodeEnum.SUCCESSED);
		Example example = new Example(OrderEntity.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", orderDTO.getUserId()).andNotEqualTo("status", CommonEnume.OrderStatus.D01.name());
		String status = orderDTO.getStatus();
		if (StringUtils.isNotBlank(status)) {
			criteria.andEqualTo("status", status);
		}
		
		List<OrderEntity> orders = orderDao.selectByExample(example);
		if (orders!=null) {
			for (OrderEntity orderEntity : orders) {
				List<OrderGoodsEntity> goodsDetails = orderGoodsDao.getGoodsDetailByOrderCode(orderEntity.getOrderCode());
				for (OrderGoodsEntity orderGoodsEntity : goodsDetails){
					ProductSkuEntity prodSku = orderGoodsEntity.getProdSku();
					String skuValue = prodSku.getSkuValue();
					if (StringUtils.isNotBlank(skuValue)) {
						prodSku.setCatModelValueEntities(prodCatModelValueDao.getModelIdByIds(skuValue));
					}
				}
				orderEntity.setGoodsDetails(goodsDetails);
			}
		}
		response.setRes_data(orders);
		return response;
	}

	@Override
	public Response modifyOrderStatus(OrderDTO orderDTO) {
		log.info("修改订单状态");
		Integer oId = orderDTO.getOId();
		OrderEntity orderEntity = new OrderEntity();
		orderEntity.setStatus(orderDTO.getStatus());
		orderEntity.setoId(oId);
		orderDao.updateByPrimaryKeySelective(orderEntity);
		
		recordOrderOperLog(oId,"用户修改订单状态");//记录订单操作日志
		
		return new Response(ResponseCodeEnum.SUCCESSED,"订单状态已更新");
	}

	@Override
	public void recordOrderOperLog(Integer oId,String describe) {
		try {
			log.info("组装订单操作信息并保存");
			OrderEntity currentOrder = orderDao.selectByPrimaryKey(oId);
			OrderOperEntity orderOper = new OrderOperEntity();
			orderOper.setoId(oId);
			orderOper.setOrderCode(currentOrder.getOrderCode());
			orderOper.setStatus(currentOrder.getStatus());
			orderOper.setPayStatus(currentOrder.getPayStatus());
			orderOper.setLogisticsStatus(currentOrder.getLogisticsStatus());
			orderOper.setOoDescribe(describe);
			orderOperDao.baseInsert(orderOper);
		} catch (Exception e) {
			log.error("订单操作日志保存失败");
			e.printStackTrace();
		}
	}

	@Override
	public Response getOrderDetails(OrderDTO orderDTO) {
		Integer oid = orderDTO.getOId();
		Response response = new Response(ResponseCodeEnum.SUCCESSED);
		OrderEntity entity = orderDao.selectByPrimaryKey(oid);
		BeanUtils.copyProperties(entity, orderDTO);
		//配送信息
		List<OrderExpressDTO> expressList = orderExpressDao.selectOrderExpressByOrder(oid);
		List<OrderGoodsEntity> goodsDetails = orderGoodsDao.getGoodsDetailByOrderCode(entity.getOrderCode());
		for (OrderGoodsEntity orderGoodsEntity : goodsDetails) {
			try {
				ProductSkuEntity prodSku = orderGoodsEntity.getProdSku();
				String skuValue = prodSku.getSkuValue();
				if (StringUtils.isNotBlank(skuValue)) {
					prodSku.setCatModelValueEntities(prodCatModelValueDao.getModelIdByIds(skuValue));
				}
			} catch (Exception e) {
				throw new MsgException(ResponseCodeEnum.SERVICE_ERROR,"商品数据异常");
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("orderInfo", orderDTO);
		map.put("orderExpress", expressList.get(0));
		map.put("goodsDetails", goodsDetails);
		response.setRes_data(map);
		return response;
	}

}